import { Controller, Get, Post, Body, Patch, Param, Delete, Logger, HttpException, HttpStatus, UnauthorizedException } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

@Controller('user')
export class UserController {
    // 注册日志方法  参数 当前控制器的name
    private logger = new Logger(UserController.name)
    constructor(private readonly userService: UserService) { 
        // 调用构造函数时打印一次
        this.logger.log('UserController 初始化完成')
    }

    @Post('/register')
    create(@Body() createUserDto: CreateUserDto) {
        return this.userService.create(createUserDto);
    }

    @Get('/get-all')
    findAll() {
        this.logger.log('调用/get-all')
        return this.userService.findAll();
    }

    @Get('/test-error')
    testError(){
        // 如果有全局过滤器 局部的会被覆盖掉
        // nest官方封装的统一报错  HttpStatus 右键查看所有的状态码 
        throw new 
        // HttpException('this is error' , HttpStatus.FORBIDDEN)
        UnauthorizedException('用户无权限')
    }

    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.userService.findOne(id);
    }

    @Patch('/update/:id')
    update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
        return this.userService.update(id, updateUserDto);
    }

    @Delete('/delete/:id')
    remove(@Param('id') id: string) {
        return this.userService.remove(id);
    }

    @Get('/profile/:id')
    findProfile(@Param('id') id :string){
        return this.userService.findProfile(id)
    }

    @Get('/logs/:id')
    findLogs(@Param('id') id :string){
        return this.userService.findLogs(id)
    }

    @Get('/group-logs/:id')
    findGroupLogs(@Param('id') id :string){
        return this.userService.findLogsByGroup(id)
    }


    // 也可以用map处理返回的值
    // @Get('/group-logs/:id')
    // async findGroupLogs(@Param('id') id :string):Promise<any>{
    //     const res = await this.userService.findLogsByGroup(id)
    //     return res.map((item) => ({
    //         code:item.code ,
    //         count:item.count
    //     }))
    // }

}
